//
//  PhotoController.m
//  HuLaQuan
//
//  Created by liyan on 16/1/30.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "PhotoController.h"

#import "TakePhotoChooseCell.h"

@interface PhotoController ()<UIImagePickerControllerDelegate>
@property(nonatomic,strong)NSArray *titiles;

@end


static NSString * const TakePhotoChooseCellIdentifier = @"TakePhotoChooseCell";

@implementation PhotoController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setupUI
{
    [self setupNavBackButton];
    self.title = @"图片选择";
    self.titiles = @[@"从图库中选择",@"拍照",@"取消"];
    
    [self.tableView registerNib:[TakePhotoChooseCell nib] forCellReuseIdentifier:TakePhotoChooseCellIdentifier];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
#pragma mark -delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else{
        return 1;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.0001;
    }else{
        return 10;
    }
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kNormalCellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TakePhotoChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:TakePhotoChooseCellIdentifier];
    
    if (indexPath.section == 0) {
        cell.titleLabel.text = [_titiles objectAtIndex:indexPath.row];
    }else{
        cell.titleLabel.text = [_titiles objectAtIndex:indexPath.row + 2];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (0 == indexPath.row) {
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            [self presentViewController:picker animated:YES completion:nil];
            
        }
        else
        {
            if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Test on real device, camera is not available in simulator" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [alert show];
                return;
            }
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES completion:nil];
            [self.imagePickerController takePicture];
        }

    }else
    {
        NSLog(@"!!!!!!%ld",indexPath.row);
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    [self.delegateViewController photoFromImagePickerView:chosenImage];
    
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
    
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    [self.view removeFromSuperview];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
